﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class ClassMarkViewModel : WorkspaceViewModel
    {
        List<StudentClassViewModel> _lstStudentClassViewModel;
        long _class;
        icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _cancelCommand;

        public ClassMarkViewModel(icanDataContext dataContext)
        {
            _lstStudentClassViewModel = new List<StudentClassViewModel>();
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_Mark_New;

        }
        public ClassMarkViewModel(icanDataContext dataContext, long classID)
        {
            _lstStudentClassViewModel = new List<StudentClassViewModel>();
            _dataContext = dataContext;
            this.Class = classID;
            base.DisplayName = Properties.Resources.Base_DisplayName_Mark_New;

        }


        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }

        private void Save()
        {
            if (_dataContext == null || _lstStudentClassViewModel == null || _lstStudentClassViewModel.Count == 0)
                return;
            foreach (StudentClassViewModel sVM in _lstStudentClassViewModel)
            {
                string[] properties = { "MidTermMark", "FinalMark" };
                foreach (string property in properties)
                {
                    string error = (sVM as System.ComponentModel.IDataErrorInfo)[property];
                    if (!string.IsNullOrEmpty(error))
                    {
                        this.ErrorMsg = string.Format("{0}: {1}", sVM.StudentName, error);
                        return;
                    }
                }
            }
            foreach (StudentClassViewModel aVM in _lstStudentClassViewModel)
            {
                aVM.Save();
            }
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }

        private void Cancel()
        {
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public long Class
        {
            get { return _class; }
            set
            {
                if (_class == value)
                    return;
                _class = value;
                base.OnPropertyChanged("Class");
                this.Marks = GetMarks();
            }
        }
        public List<ClassViewModel> Classes
        {
            get
            {
                return _dataContext.Classes.Where(oClass => oClass.EndDate >= DateTime.Today.AddMonths(-1)).Select(oClass => new ClassViewModel(_dataContext, oClass)).ToList();
            }
        }
        public List<StudentClassViewModel> Marks
        {
            get { return _lstStudentClassViewModel; }
            set
            {
                if (_lstStudentClassViewModel == value || this.Class == 0)
                    return;
                _lstStudentClassViewModel = value;
                base.OnPropertyChanged("Marks");
            }
        }
        public List<StudentClassViewModel> GetMarks()
        {
            List<StudentClassViewModel> lstSCVM = _dataContext.StudentClasses.Where(st => st.Class == this.Class).Select(sc => new StudentClassViewModel(_dataContext, sc)).ToList();
            return lstSCVM;
        }

    }
}
